package com.example.myapplication.tack_15;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.myapplication.R;
import com.example.myapplication.tack_15.adapter.ArrangeAdapter;
import com.example.myapplication.tack_15.adapter.CommentAdapter;
import com.example.myapplication.tack_15.adapter.LawterXQAdapter;
import com.example.myapplication.tack_15.data.ArrangeData;

import com.example.myapplication.tack_15.data.CommentData;
import com.example.myapplication.tack_15.data.LawyerData;
import com.example.myapplication.tack_15.data.LawyerXQData;
import com.example.myapplication.utils.Common;
import com.example.myapplication.utils.OkHttpTool;
import com.google.android.material.tabs.TabLayout;

import java.util.ArrayList;
import java.util.List;

public class LawyerQXActivity extends AppCompatActivity {
    private ImageView exit;
    private List<ArrangeData.RowsDTO> listArrange;
    private ImageView avatarUrl;
    private TextView name;
    private TextView serviceTimes;
    private TextView legalExpertiseName;
    private TextView baseInfo;
    private TabLayout tab;
    private List<String> listTab;
    private RecyclerView recy;
    private TextView free;
    private ArrangeData arrangeData;
    private List<CommentData.RowsDTO> listComment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tack_15_activity_lawyer_q_x);
        init();
        Intent intent = getIntent();
        String id = intent.getStringExtra(Common.COMMON);
        exit_();
        getArrange(id);
        getContact(id);
        tab_(id);
        free_();
    }

    private void free_() {
        free.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(LawyerQXActivity.this,FieeActivity.class);
                LawyerQXActivity.this.startActivity(intent);

            }
        });
    }

    private void tab_(String str) {
        listTab = new ArrayList<>();
        listTab.add("服务方式");
        listTab.add("用户评价");
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                for (String s : listTab) {
                    TabLayout.Tab tt = tab.newTab();
                    tt.setText(s);
                    tab.addTab(tt);
                }
                tabAdapter(str);

            }
        });
    }

    private void tabAdapter(String id) {
        tab.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                int position = tab.getPosition();
                if (position == 0) {
                    getContact(id);
                } else {
                    getComment(id);
                }
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }

    private void getComment(String id) {
        String url = "prod-api/api/lawyer-consultation/lawyer/list-evaluate?lawyerId="+id+"&pageSize=10&pageNum=1";
        OkHttpTool.doNetRequest(url, OkHttpTool.GET, CommentData.class, new OkHttpTool.NetCallback() {
            @Override
            public void onSuccess(Object obj) {
                CommentData comment = (CommentData)obj;
                if(comment.getCode() == 200){
                    listComment = comment.getRows();
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            free.setVisibility(View.VISIBLE);
                            commontAdapter();
                        }
                    });
                }
            }

            @Override
            public void onFailure(String msg) {

            }
        });
    }
    private void commontAdapter(){
        CommentAdapter commentAdapter = new CommentAdapter(listComment, LawyerQXActivity.this);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(LawyerQXActivity.this);
        recy.setAdapter(commentAdapter);
        recy.setLayoutManager(linearLayoutManager);
    }
    private void getContact(String id) {
        String url = "prod-api/api/lawyer-consultation/lawyer/"+id;
        OkHttpTool.doNetRequest(url, OkHttpTool.GET, LawyerXQData.class, new OkHttpTool.NetCallback() {
            @Override
            public void onSuccess(Object obj) {
                LawyerXQData lawyerXQ = (LawyerXQData) obj;
                if (lawyerXQ.getCode() == 200) {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            free.setVisibility(View.GONE);
                            ArrAdapter(lawyerXQ);
                        }
                    });
                }
            }

            @Override
            public void onFailure(String msg) {

            }
        });
    }
    private void ArrAdapter(LawyerXQData lawyerXQ) {
        LawterXQAdapter lawterXQAdapter = new LawterXQAdapter(lawyerXQ, LawyerQXActivity.this);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(LawyerQXActivity.this);
        recy.setLayoutManager(linearLayoutManager);
        recy.setAdapter(lawterXQAdapter);
    }



    private void getArrange(String id) {
        String url = "prod-api/api/lawyer-consultation/lawyer/"+id;
        OkHttpTool.doNetRequest(url, OkHttpTool.GET, LawyerData.class, new OkHttpTool.NetCallback() {
            @Override
            public void onSuccess(Object obj) {
                LawyerData arrangeData = (LawyerData)obj;
                if(arrangeData.getCode() == 200){
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Glide.with(LawyerQXActivity.this).load(OkHttpTool.URL
                                    + arrangeData.getData().getAvatarUrl()).into(avatarUrl);
                            name.setText(arrangeData.getData().getName());
                            serviceTimes.setText("咨询人数:"+String.valueOf(arrangeData.getData().getServiceTimes()));
                            legalExpertiseName.setText("法律专长:"+arrangeData.getData().getLegalExpertiseName());
                            baseInfo.setText("介绍:"+arrangeData.getData().getBaseInfo().substring(0,40)+"...");
                        }
                    });
                }
            }

            @Override
            public void onFailure(String msg) {

            }
        });
    }



    private void exit_() {
        exit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                LawyerQXActivity.this.finish();
            }
        });
    }

    private void init() {

        exit = findViewById(R.id.exit);
        avatarUrl = findViewById(R.id.avatarUrl);
        name = findViewById(R.id.name);
        serviceTimes = findViewById(R.id.serviceTimes);
        legalExpertiseName = findViewById(R.id.legalExpertiseName);
        baseInfo = findViewById(R.id.baseInfo);
        tab = findViewById(R.id.tab);
        recy = findViewById(R.id.recy);
        free = findViewById(R.id.free);
    }
}